layui.config({
    base: hostUrl + '/dms-web/static/layuiadmin/'
}).extend({
    index: 'lib/index', //主入口模块
}).use(['index', 'table', 'layer', 'form'], function () {
    var $ = layui.jquery
        , layer = layui.layer
        , form = layui.form
        , table = layui.table

    var currPage = 1;
    //角色列表
    var role = table.render({
        elem: '#roleManger'
        , cols: [[ //标题栏
            {field: 'roleName', title: '角色名称'}
            , {field: 'tool', title: '操作', toolbar: '#roleMangerBar'}
        ]]
        , height: 500
        , cellMinWidth: 120
        , url: hostUrl + '/sys/queryRoles'
        , page: true //是否显示分页
        , id: 'roleListReload'
        , done: function (res, curr, count) {
            //如果是异步请求数据方式，res即为你接口返回的信息。
            //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
            //得到当前页码
            currPage = curr;
            $('.layui-table-fixed-r').removeClass('layui-hide');
        }
    });

    //监听role表格工具条
    table.on('tool(roleListTable)', function (obj) {
        var data = obj.data;
        if (obj.event === 'edit') {
            var roleId = data.id;
            var editRoleIndex = layer.open({
                type: 2
                , id: (new Date()).valueOf()
                , title: "编辑角色"
                , content: hostUrl + '/sys/role/editRole.do?roleId=' + roleId
                , area: ['800px', '600px']
                , btn: ['保存', '取消']
                , maxmin: true
                , success: function (layero, index) { // 请注意此处有坑
                    var body = layer.getChildFrame('body', index);
                    // 回显角色名称
                    body.find("input[name='roleName']").val(data.roleName);
                }
                , yes: function (index, layero) { // 请注意此处有坑
                    var iframeWin = window[layero.find('iframe')[0]['name']];
                    // 保存数据
                    iframeWin.edit();
                    return false;
                }
                , btn2: function (index, layero) {
                    //layer.alert("取消按钮")
                    //return false 开启该代码可禁止点击该按钮关闭
                },end : function() {
                    location.reload();
                }
            })
            layer.full(editRoleIndex);
        } else if (obj.event === 'delete') {
            var roleId = obj.data.id;
            Layer.showConfirm('确定删除吗?', function (index) {
                $.axse(hostUrl + "/sys/deleteRole",
                    {"id": roleId},
                    function (res) {
                        if (res.status == 0) {
                            if (res.data == 1) {
                                Layer.closeLayer(index);
                                Layer.showSucMsg();
                                table.reload('roleListReload');
                            }
                        } else {
                            Layer.showFailMsg(res.msg, 0);
                        }
                    },
                    function () {
                        Layer.showFailMsg("系统网络开小差了，请稍后重试");
                    })

            });
        }
    });

    // 新建角色绑定事件
    $("#addRole").on("click", function () {
        newRoles()
    })

    function newRoles() {
        top.layer.prompt({title: '请输入角色名称，并确认'}, function (roleName, index) {
            top.layer.close(index);
            $.axse(hostUrl + "/sys/addRole",
                {"roleName": roleName},
                function (res) {
                    if (res.status == 0) {
                        if (res.data == 1) {
                            top.layer.close(index);
                            Layer.showSucMsg();
                            table.reload('roleListReload');
                        }
                    } else {
                        Layer.showFailMsg(res.msg, 0);
                    }
                },
                function () {
                    Layer.showFailMsg("系统网络开小差了，请稍后重试");
                });
        });
    }

    // 编辑角色保存
    function edit() {
        var roleId = roleId;
        var roleName = $("input[name='roleName']").val();
        var remark = $("input[name='remark']").val();
        var menuIds = $.tree.getCheckedNodes();
        $.axse(hostUrl + "/sys/roleEdit",
            {
                "roleId": roleId,
                "roleName": roleName,
                "description": remark,
                "menuIds": menuIds
            },
            function (res) {
                if (res.status == 0) {
                    Layer.showSucMsg();
                } else {
                    Layer.showFailMsg(res.msg, 0);
                }
            },
            function () {
                Layer.showFailMsg("系统网络开小差了，请稍后重试");
            });
    }

    //给方法绑定click事件
    $('.layadmin-takerates .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    form.render();
});